//
//  BaseTableViewController.swift
//  CBWWeiBo
//
//  Created by 陈博文 on 16/1/8.
//  Copyright © 2016年 陈博文. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController {

    //定义一个属性,设置可选类型
    var vistorView:VisitorView? = nil
    
    //判断是否登录
    var isLogin : Bool = UserAccount.isLogon()
    
// MARK:=============== viewDidLoad ===============
    override func viewDidLoad() {
        isLogin ? super.viewDidLoad() : setupVisitorView()
        
    }
// MARK:=============== setupVisitorView ===============
  private func setupVisitorView () {
    //创建访客视图
    let vistorView = VisitorView.victor()
    self.vistorView = vistorView
    
    view = vistorView
    //设置 button 的监听
    self.vistorView?.registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
    self.vistorView?.loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
    setupNavi()
    }
    
   // MARK:=============== setupIconWithImage ===============
    //在父类中定义,在子类中调用
    func setupVistorViewIconWithLabel(iconName:String,labelText:String){
    //拿到 vistorView,对其里面的属性进行赋值
    vistorView?.iconImageView.image = UIImage(named: iconName)
    vistorView?.titleLabel.text = labelText
    vistorView?.rotateImageView.hidden = true
    }
    
    
    // MARK:=============== button 点击方法的监听 ===============
    
    @objc private func registerButtonClick(){
        print("registerButtonClick")
    }
    @objc private func loginButtonClick(){
       loginBtnClick()
    }
    
    // MARK:=============== 设置图片旋转 ===============
    
    func setupRotate(){
        //设置动画属性
        let animate = CABasicAnimation()
        animate.keyPath     = "transform.rotation"
        animate.repeatCount = MAXFLOAT
        animate.fromValue   = 0
        animate.toValue     = M_PI * 2
        animate.duration    = 10
        
        //页面切换回来,动画不会消失
        animate.removedOnCompletion = false
        
        vistorView?.rotateImageView.layer.addAnimation(animate, forKey: nil)
    }
    
    private func setupNavi(){
        
        // 3.在NavigationBar左右两边添加item
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Done, target: self, action: "registerBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Done, target: self, action: "loginBtnClick")

    }
    
  @objc  private func registerBtnClick(){
        print("registerBtnClick")
    }

    @objc  private func loginBtnClick(){
        // 1.创建授权页面
        let oauthVc = OAuthViewController()
        
        // 2.包装导航控制器
        let oauthNav = UINavigationController(rootViewController: oauthVc)
        
        // 3.弹出控制器
        presentViewController(oauthNav, animated: true, completion: nil)
    }

   
}

